<?php


namespace app\api\controller;

use app\api\model\Base as bs;
use think\Db;
use think\Request;

class Takecourse extends Base
{
    public function takecourse()
    {
        if (Request::instance()->method() == "GET") {
            $page = input('current','1','intval');
            $page_size = input('pageSize',10,'intval');
            $id = I('id');
            $like = I("course_name|department_name|people_num|time|classroom_name|teacher_name");
            $data = Db::name('course')
                ->alias('a')
                ->join('course_department b', 'a.id = b.course_id')
                ->join('department c', 'b.department_id = c.id')
                ->join('course_classroom d', 'a.id = d.course_id')
                ->join('classroom e', 'd.classroom_id = e.id')
                ->join('user f', 'a.teacher_id = f.id', 'INNER')
                ->field(['e.id' => 'classroom_id', 'e.name' => 'classroom_name'])
                //->field('a.course_name,a.people_num,a.time,c.id,c.department_name,e.id,e.name')->field(['a.id'=>'key'])
                ->field('a.course_name,a.people_num,a.time,c.department_name')
                ->field(['a.id' => 'key', 'c.id' => 'department_id'])
                ->field(['f.username' => 'teacher_name'])
                ->where('a.course_name', 'like', '%'.$like['course_name'].'%')
                ->where('c.department_name', 'like', '%'.$like['department_name'].'%')
                ->where('a.people_num', 'like', '%'.$like['people_num'].'%')
                ->where('a.time', 'like', '%'.$like['time'].'%')
                ->where('e.name', 'like', '%'.$like['classroom_name'].'%')
                ->where('f.username', 'like', '%'.$like['teacher_name'].'%')
                ->page($page,$page_size)
                ->where('a.status', 1)
                ->select();
            $user = Db::name('user_course')
                ->alias('a')
                ->where('a.uid', $id)
                ->where('a.status', 1)
                ->field(['a.course_id' => 'key'])
                ->select();
// 某大牛写的sql
//            select a.id course_name,c.id department_id,a.course_name,a.people_num,a.time,c.department_name,
//                e.name classroom_name,case when b.id != null then true else false end flag from course a
//                left join user_course b on a.id = b.course_id and a.status = 1 and a.uid = 123
//                left join course_department c on a.id = c.course_id
//                left join department d on c.department_id = d.id
//                left join course_classroom e on a.id = e.course_id
//                left join classroom f on e.classroom_id = f.id
//                where a.status = 1
            foreach ($data as $k => &$v) {
                foreach ($user as $user_k => $user_v) {

                    if ($v['key'] == $user_v['key']) {
                        $data[$k] = array_merge(['flag' => true], $data[$k]);
                    } else {
//                        $data[$k] = array_merge(['flag' => false],$data[$k]);
                    }
                }
            }
            $total = Db::name('course')
                ->alias('a')
                ->join('course_department b', 'a.id = b.course_id')
                ->join('department c', 'b.department_id = c.id')
                ->join('course_classroom d', 'a.id = d.course_id')
                ->join('classroom e', 'd.classroom_id = e.id')
                ->join('user f', 'a.teacher_id = f.id', 'INNER')
                ->where('a.status', 1)
                ->count();
            $this->apiReturn(200, '', $data, count($data));
        }
    }

}